//21   B  Force sit on an object : @sit:<UUID>=force (*) 


integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
list gMenu=[];
list statesMenu=[0,0,0,0,0,0,0,0];
list gAvatarAllow;
key ToucherID;
integer gMenuLink =1337;
integer gDemon =1338;
integer gVic =1329;
integer gPlugin =1339;

integer channel_dialog;
integer listen_id;
integer listen_id2;
integer channel_dialog2=13337;
string msg ="Force Sit";
list sideMenu=[" ","Back"," "];
integer range =19;
integer gPage=0;
list objectKey=[];
list pageMenu(list in ,integer page)
{
	list out;
	integer maxpages =llGetListLength(in)/9;
	page = (page)%(maxpages+1);
	out =["<<","[Back]",">>"];
	integer _listcount =(page *9)+9;
	integer _i=_listcount-9;
	
	for (_i;_i<_listcount;_i++)
	{
		if(_i<=llGetListLength(in)&&llList2String(in,_i)!="")
		out+=llList2String(in,_i);
	}
	return out;
}

llRlvSay(string command)
{
	if(attachment==TRUE)
	{
		llOwnerSay("@"+command);
	}
	else
	{
		llSay(relaychannel,cmdname + "," + (string)victim  + "," + "@"+command);
	}
}



llLRP(integer element_new, integer placeinlist)
{
	statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

makeForceSit()
{
	
	llRlvSay("getsitid="+(string)channel_dialog2); 
	listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
}

displayMenu()
{
	list _menu;
	list _manipulation; 
	if(llGetListLength(gMenu)<10)
	{
		_menu= [" ","[Back]", " "]+gMenu;
	}
	else
	{
		_menu= pageMenu(gMenu,gPage);
	}
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,_menu, channel_dialog);
	llSetTimerEvent(45.0);
}

default {
    state_entry() 
    {
    	    
    }
    
    listen(integer channel, string name, key id, string message)
    {
    	if(channel==channel_dialog)
    	{
	    	llListenRemove(listen_id);
			llSetTimerEvent(0.0);
			if(message=="<<")
	  		{
	  			gPage--;
				integer maxpages =llGetListLength(gMenu)/9;
				if(gPage<0)
				{
					gPage = maxpages;
				}
				displayMenu();
	  		}
	  		else if(message==">>")
	  		{
	  			gPage++;
	  			displayMenu();
	  		}
			else if(message=="[Back]")llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_RLV", ToucherID);
			else if(message=="Unsit")
			{
				llRlvSay("unsit=force");
				llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_RLV", ToucherID);
			}
			else if(~llListFindList(gMenu, (list)message))
			{
				string _objectKey = llList2String(objectKey, (integer)message);
				llRlvSay("sit:"+_objectKey+"=force");
				llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_RLV", ToucherID);
			}
    	}
    	if(channel==channel_dialog2)
    	{
    		llListenRemove(listen_id2);
    		llSetTimerEvent(0.0);
    		if(message==NULL_KEY)
    		{
	    		llSensor("", NULL_KEY, SCRIPTED, range, TWO_PI);
    		}
    		else 
    		{
    			msg="Avatar Sit's";
    			gMenu=["Unsit"];
    			displayMenu();
    		}
    	}	
    }
    
    on_rez(integer on)
    {
    
    }
    sensor(integer num)
    {
        //give menu of potential victims
        integer n;
        gMenu = [];
		objectKey=[];
		msg="";
        for (n = 0; n < num; ++n)
        {
        	objectKey+= llDetectedKey(n);
            msg +=(string)n+": "+ llDetectedName(n)+"\n";
            gMenu+=(string)n;
        }
        displayMenu();
    }
    link_message(integer sender, integer num, string str, key id)
  	{
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				makeForceSit();
  			}
  		}

  		if(num ==gDemon)
  		{
  			if(str =="attachment")attachment=TRUE;
  			else if(str =="device")attachment=FALSE;
  			else if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
				llListenRemove(listen_id2);
  			}
  		}
  		if(num==gPlugin)
  		{
  			if(str=="RESET")
  			{
  				statesMenu=[0,0,0,0,0,0,0,0];
  				objectKey=[];
  			}
  		}
  		if(num==gVic)
  		{
  				victim=(key)str;
  				cmdname = (string)llGetKey();
  		}
  	}
  	timer()
  	{
  		llListenRemove(listen_id);
		llListenRemove(listen_id2);
  	}
}
